//
//  SecondViewController.m
//  MvcTest
//
//  Created by SDMac on 15/5/11.
//  Copyright (c) 2015年 SDMac. All rights reserved.
//

#import "SecondViewController.h"

#import "MJRefresh.h"

#import "BaseRequest.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

#pragma mark ---- life cycle

- (void)loadView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenBunds.size.width, ScreenBunds.size.width)];
    
    self.view = view;
    
    self.view.backgroundColor = [UIColor grayColor];
    
    self.mainTable = [[UITableView alloc]initWithFrame:ScreenBunds];
    self.mainTable.dataSource = self;
    self.mainTable.delegate = self;
    [self.view addSubview:self.mainTable];
    
    [self.mainTable.header beginRefreshing];

    [self.mainTable addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(addHeader)];
    
    [self.mainTable addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(addFooter)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.title = @"第二";
    
    _pagenum = 1;
    
    self.netAry = [NSMutableArray arrayWithCapacity:0];
    
    __weak __typeof(self) weakSelf = self;
    
    BaseRequest *sendRequest = [[BaseRequest alloc] init];
    
    [sendRequest searchVideoWithKeyWord:@"火影忍者" pagesNumber:_pagenum success:^(AFHTTPRequestOperation *operation, id responseObject){
        
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        
        NSDictionary *dic = (NSDictionary *)responseObject;
        
        [strongSelf.netAry setArray:[dic objectForKey:@"results"]];
        
        [strongSelf.mainTable reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告"
                                                           message:@"链接错误"
                                                          delegate:nil
                                                 cancelButtonTitle:@"确定"
                                                 otherButtonTitles:nil];
        [alertView show];
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    RootViewController *root=(RootViewController *)self.tabBarController;
    [root setTabarHiend:NO];
    self.navigationController.navigationBarHidden=NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ---- UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.netAry.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"cellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *videoDic = [self.netAry objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"标题:%@",[videoDic objectForKey:@"title"]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"信息"
                                                       message:cell.textLabel.text
                                                      delegate:nil
                                             cancelButtonTitle:@"确定"
                                             otherButtonTitles:nil];
    [alertView show];
}

#pragma mark ---- CustomDelegate

#pragma mark ---- event response

#pragma mark ---- private methods

- (void)addHeader//下拉（刷新）
{
    __weak __typeof(self) weakSelf = self;
    
    _pagenum = 1;
    
    BaseRequest *sendRequest = [[BaseRequest alloc] init];
    
    [sendRequest searchVideoWithKeyWord:@"火影忍者" pagesNumber:_pagenum success:^(AFHTTPRequestOperation *operation, id responseObject){
        
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        
        NSDictionary *dic = (NSDictionary *)responseObject;
        
        [strongSelf.netAry setArray:[dic objectForKey:@"results"]];
        
        [strongSelf.mainTable reloadData];
        [strongSelf.mainTable.header endRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.mainTable.header endRefreshing];
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告"
                                                           message:@"链接错误"
                                                          delegate:nil
                                                 cancelButtonTitle:@"确定"
                                                 otherButtonTitles:nil];
        [alertView show];
    }];
}

- (void)addFooter//上拉（加载）
{
    __weak __typeof(self) weakSelf = self;
    
    _pagenum ++;
    
    BaseRequest *sendRequest = [[BaseRequest alloc] init];
    
    [sendRequest searchVideoWithKeyWord:@"苏联" pagesNumber:_pagenum success:^(AFHTTPRequestOperation *operation, id responseObject){
        
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        
        NSDictionary *dic = (NSDictionary *)responseObject;
        
        [strongSelf.netAry addObjectsFromArray:[dic objectForKey:@"results"]];
        
        [strongSelf.mainTable reloadData];
        
        [strongSelf.mainTable.footer endRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        
        [strongSelf.mainTable.footer endRefreshing];
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告"
                                                           message:@"链接错误"
                                                          delegate:nil
                                                 cancelButtonTitle:@"确定"
                                                 otherButtonTitles:nil];
        [alertView show];
    }];
}


#pragma mark ---- getters and setters

@end
